﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Album_action : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string act=string.IsNullOrEmpty(Request["action"])?Request["myaction"]:Request["action"];
        if (!string.IsNullOrEmpty(act)) {
            var alb_bl = new Com.Ooohd.Bll.Img.ZS_Album();
            var img_bl = new Com.Ooohd.Bll.Img.ZS_Image();
            switch (act) { 
                case "album_add":
                    if (string.IsNullOrEmpty(Request["albumname"])) {
                        Response.Write(Common.JSONHelper.ObjectToJSON(new {succ=false,msg="请输入相册名称" }));
                        return;
                    }   
                    var alb = alb_bl.CreateModel();
                    alb.AlbumName = Request["albumname"].Trim();
                    alb.CreateDate = DateTime.Now;
                    alb.ImgCount = 0;
                    alb_bl.Add(alb);
                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ=true}));
                    break;
                case "album_edit":
                    if (string.IsNullOrEmpty(Request["albumname"])) {
                        Response.Write(Common.JSONHelper.ObjectToJSON(new {succ=false,msg="请输入相册名称" }));
                        return;
                    }  
                    alb=alb_bl.GetSingle(int.Parse(Request["aid"]));
                    alb.AlbumName = Request["albumname"].Trim();
                    alb_bl.Update(alb);
                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = true }));
                    break;
                case "del":
                    if (string.IsNullOrEmpty(Request["ids"]))
                    {
                        Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = false, msg = "未选中任何待删除记录。" }));
                        return;
                    } 
                    alb_bl.Delete(Request["ids"].TrimEnd(','));
                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = true }));
                    break;
                case "save_file":
                    if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
                    {
                        var file=Request.Files[0];
                        var finfo = new FileInfo(file.FileName);
                        if (".jpg,.png,.gif".Contains(finfo.Extension.ToLower()))
                        {
                            if (file.ContentLength / 1024 <= 1024)
                            {
                                int albumID = int.Parse(Request["aid"]);
                                string filename = Guid.NewGuid().ToString() + finfo.Extension;
                                string msg = "";
                                var buff=new byte[file.ContentLength];
                                file.InputStream.Read(buff,0,file.ContentLength);

                                var b = new com.ooohd.img.WebService().SaveImg(buff, filename, albumID, ref msg);
                                if (b) {
                                    var img=img_bl.CreateModel();
                                    img.AlbumID = albumID;
                                    img.CreateDate = DateTime.Now;
                                    img.ImgName=Request["imgname"];
                                    img.FileName = filename;
                                    img_bl.Add(img);
                                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = true }));
                                }else
                                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = false, msg = "上传图片发生意外。"+msg }));
                            }
                            else { 
                                Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = false, msg = "请上传1MB内的图片。" }));
                            }
                        }
                        else {
                            Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = false, msg = "请上传jpg、png或gif格式图片。" }));
                        }
                    }
                    else {
                        Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = false,msg="请先选择图片。" }));
                    }
                    break;
                case "img_del":
                    if (string.IsNullOrEmpty(Request["ids"]))
                    {
                        Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = false, msg = "未选中任何待删除记录。" }));
                        return;
                    }
                    var ws=new com.ooohd.img.WebService();
                    string delids = "";
                    foreach (var item in Request["ids"].TrimEnd(',').Split(','))
                    {
                        var img = img_bl.GetSingle(int.Parse(item));
                        try
                        {
                            
                            string msg = "";
                            if (ws.DelImg(img.FileName, img.AlbumID, ref msg))
                            {
                                img_bl.Delete(img.ID);
                                delids += item + ",";
                            }
                        }
                        catch (Exception ex) {
                            Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = false,ids=delids.TrimEnd(',').Split(','), msg = "图片【"+img.ImgName+"】删除失败。" }));
                            return;
                        }
                    }
                    Response.Write(Common.JSONHelper.ObjectToJSON(new { succ = true}));
                    break;
            }
        }
    }

    
}
